package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.OverflowService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: OverflowService
 * Package: com.atguigu.jxc.service.impl
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/21 - 22:30
 * @Version: v1.0
 */
@Service
public class OverflowServiceImpl implements OverflowService {

    @Autowired
    private OverflowDao overflowDao;
    @Autowired
    private LogService logService;

    @Override
    public ServiceVO save(OverflowList overflowList, String overflowListGoodsStr, HttpSession session) {

        if (overflowListGoodsStr == null || overflowListGoodsStr.isEmpty()) {
            return new ServiceVO<>(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        if (overflowList == null) {
            return new ServiceVO<>(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        // 查询所有报溢单的数量，然后加1作为报溢单的ID
        Integer overflowCount = overflowDao.countOverflowList() + 1;
        Gson gson = new Gson();
        // 从session中获取当前登录用户的ID
        User user = (User) session.getAttribute("currentUser");
        overflowList.setUserId(user.getUserId());
        overflowDao.saveOverflowList(overflowList);
        List<OverflowListGoods> overflowListGoodsList = gson.fromJson(overflowListGoodsStr, new TypeToken<List<OverflowListGoods>>() {
        }.getType());
        overflowListGoodsList.forEach(overflowListGoods -> {
            overflowListGoods.setOverflowListId(overflowCount);
            overflowDao.saveDamageListGoods(overflowListGoods);
        });
        logService.save(new Log(Log.INSERT_ACTION, "新增报溢单"));
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        if (sTime == null || eTime == null) {
            // 查询全部报溢单
            List<OverflowList> damageLists = overflowDao.list(sTime, eTime);
            Map<String, Object> map = new HashMap<>();
            map.put("rows", damageLists);
            return map;
        }
        // 查询所有报溢单 范围是startTime到endTime
        List<OverflowList> damageLists = overflowDao.list(sTime, eTime);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", damageLists);
        logService.save(new Log(Log.SELECT_ACTION, "查询报溢单"));
        return map;
    }

    @Override
    public Map<String, Object> goodsLis(Integer overflowListId) {
        if (overflowListId == null) {
            return Collections.emptyMap();
        }
        List<OverflowListGoods> overflowListGoods = overflowDao.goodsList(overflowListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", overflowListGoods);
        logService.save(new Log(Log.SELECT_ACTION, "查询报溢单详情"));
        return map;
    }
}
